<template>
  <div class="execution">
    <basic-container>
      <avue-crud ref="crud"
                 :page="page"
                 :data="tableData"
                 :table-loading="tableLoading"
                 :option="tableOption"
                 @row-save="handleSave"
                 @row-update="handleUpdate"
                 @row-del="handleDelete"
                 @on-load="getList"
                 @refresh-change="refreshChange"
                 @search-change="searchChange">

      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
  import {fetchList, handleDown,fetchDataSource,addObj,updateObj,delObj} from '@/api/gen/data-source-config'
  import {tableOption} from '@/const/crud/gen/data-source-config'
  import {mapGetters} from 'vuex'

  export default {
    name: 'dataSourceConfig',
    data() {
      return {
        tableData: [],
        formData: {},
        box: false,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        dataSourceName: '',
        tableLoading: false,
        tableOption: tableOption
      }
    },
    created() {
    },
    mounted: function () {
      console.log(this.dataSourceNames)
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      getList(page, params) {
        this.tableLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params)).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      //添加
      handleSave: function (row, done) {
        addObj(row).then(data => {
          this.tableData.push(Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          this.getList(this.page)
          done()
          this.$emit('par', 'loadDataSource');
        }).catch(function () {
          done()
        })
      },
      //修改
      handleUpdate: function (row, index, done) {
        updateObj(row).then(data => {
          this.tableData.push(Object.assign({}, row))
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          this.getList(this.page)
          done()
          this.$emit('par', 'loadDataSource');
        }).catch(function () {
          done()
        })
      },
      //修改
      handleDelete: function (row, done) {
        var _this = this
        this.$confirm('是否确认删除"' + row.remark + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(row)
        }).then(() => {
          this.getList(this.page)
          _this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
          this.$emit('par', 'loadDataSource');
        }).catch(function () {
        })
      },
      handleDown: function (row, index) {
        this.formData.tableName = row.tableName
        this.box = true
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getList(this.page)
      },
      searchChange(form) {
        this.getList(this.page, form)
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>

